package com.niit.controller;

import com.niit.apis.JobFeignApi;
import com.niit.entity.Job;
import com.niit.entity.User;
import com.niit.utils.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/*
 * 职位管理接口
 */

@RestController
@RequestMapping("/api/job")
public class JobController {
    @Autowired
    private JobFeignApi jobFeignApi;


    //发布职位
    @PostMapping("/add")
    public Result<Object> addJob(@RequestBody Job job, HttpSession session) {
        try {
            User user = (User) session.getAttribute("account");
            job.setUser(user);
            jobFeignApi.addJob(job);
            return Result.success();
        }catch (Exception ignored){}
        return Result.error("服务器出错");
    }

    //修改职位信息
    @PutMapping("/update")
    public Result<Object> updateJob(@RequestBody Job job, HttpSession session) {
        try {
            User user = (User) session.getAttribute("account");
            job.setUser(user);
            jobFeignApi.updateJob(job);
            return Result.success();
        }catch (Exception ignored){}
        return Result.error("服务器出错");
    }

    //删除职位信息
    @DeleteMapping("/delete/{id}")
    public Result<Object> deleteJob(@PathVariable("id") Integer id) {
        jobFeignApi.deleteJob(id);
        return Result.success();
    }

    //获取职位信息
    @GetMapping("/get/{id}")
    public Result<Object> getJob(@PathVariable("id") Integer id) {
        jobFeignApi.getJob(id);
        return Result.success();
    }

    //获取所有职位信息
    @GetMapping("/getAll")
    public Result<Object> getAllJobs() {
        return Result.success(jobFeignApi.getAllJobs());
    }

    //获取个人发布的职位信息
    @GetMapping("/getJobs")
    public Result<Object> getMyJobs(@RequestBody User user) {
        return Result.success(jobFeignApi.getMyJobs(user));
    }

    //获取个人发布的职位数量
    @GetMapping("/getJobCount")
    public Result<Object> getJobCount(@RequestBody User user) {
        return Result.success(jobFeignApi.getJobCount(user));
    }

    //搜索职位信息
    @GetMapping("/search")
    public Result<Object> searchJob(String keyword) {
        return Result.success(jobFeignApi.searchJob(keyword));
    }

    // 多条件分页查询职位信息
    @GetMapping("/page")
    public Result<Object> getJobsByCondition(
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "publishUser", required = false) String publishUser,
            @RequestParam(value = "status", required = false) Byte status,
            @RequestParam(value = "page", defaultValue = "1") int page,
            @RequestParam(value = "limit", defaultValue = "10") int limit) {

        try {
            Page<Job> jobs = jobFeignApi.getJobsByCondition(title, publishUser, status, page,limit);

            // 转换为前端需要的格式
            return Result.successPage(jobs.getContent(), jobs.getTotalElements());
        } catch (Exception e) {
            return Result.error(500, "查询失败: " + e.getMessage());
        }
    }

    @PutMapping("/audit")
    public Result<Object> auditJob(@RequestParam("id") Integer id, @RequestParam("status") Byte status) {
       jobFeignApi.auditJob(id, status);
        return Result.success();
    }

    @GetMapping("/auditedJobs")
    public Result<Object> getAuditedJobs() {
        return Result.success(jobFeignApi.getAuditedJobs());
    }
}
